package il.ac.technion.wiki.impl.tests;

import junit.framework.TestCase;
import org.junit.Test;
import il.ac.technion.wiki.api.IContentRanker;
import il.ac.technion.wiki.api.IDataStore.DataStoreException;
import il.ac.technion.wiki.api.IDataStore.NameExistsException;
import il.ac.technion.wiki.api.IDataStore.NotFoundException;
import il.ac.technion.wiki.api.IWikiEngine.IWikiContent;
import il.ac.technion.wiki.api.search.AndSearchExpression;
import il.ac.technion.wiki.api.search.ISearchExpression;
import il.ac.technion.wiki.api.search.NotSearchExpression;
import il.ac.technion.wiki.api.search.OrSearchExpression;
import il.ac.technion.wiki.api.search.TextSearchExpression;
import il.ac.technion.wiki.impl.WikiRanker;

public class TestWikiRanker extends TestCase{
	
	IWikiContent getWikiContent(String name, String text, int revision){
		return new WikiContentStub(name, text, revision);
	}
	
	
	@Test
	public void testSearch1() throws NotFoundException, NameExistsException, DataStoreException{	
		IContentRanker wr = new WikiRanker();
		String[] words = {"the", "and", "is", ".", ",", "?", "!", "(", ")", "\"", "\'", "-", ":", "\t", "\n"}; 
		wr.setNeutralWords(words);
		
		IWikiContent[] contents = {getWikiContent("A", "new york 0", 0), 
				getWikiContent("A", "New Yor----", 0), 
				getWikiContent("A", "Haifa, Tel Aviv", 0), 
				getWikiContent("A", "NeW!.......and			\n\n\n---------\n\n\n YoRk, , sgdfgsdfg  			", 0), 
				getWikiContent("A", "new york new york new york new york new york new york", 0), 
				getWikiContent("A", "the white, the blue,....( and the red )", 0), 
				getWikiContent("A", "red blue", 0), 
				getWikiContent("A", "blue red", 0)};
		{
			ISearchExpression expr = new TextSearchExpression("new york");
			int[] scores = {1, 0, 0, 1, 6, 0, 0, 0};
			for (int i=0; i < scores.length; ++i )
				assertEquals(scores[i], wr.rank(contents[i], expr));
		}
		{
			ISearchExpression expr = new TextSearchExpression("blue red");
			int[] scores = {0, 0, 0, 0, 0, 1, 0, 1};
			for (int i=0; i < scores.length; ++i )
				assertEquals(scores[i], wr.rank(contents[i], expr));
		}		
	}	
	
	
	@Test
	public void testSearch2() throws NotFoundException, NameExistsException, DataStoreException{	
		IContentRanker wr = new WikiRanker();
		String[] words = {"the", "and", "is", ".", ",", "?", "!", "(", ")", "\"", "\'", "-", ":", "\t", "\n"}; 
		wr.setNeutralWords(words);
		
		IWikiContent[] contents = {getWikiContent("A", "0 00 TTTTT TTTTT new york 0", 0), 
				getWikiContent("A", "TTTT New Yor---- 00000 00 NEW", 0), 
				getWikiContent("A", "Haifa, Tel Aviv, 0000", 0), 
				getWikiContent("A", "NeW!.......and			\n\n\n---------\n\n\n YoRk, TTTT, sgdfgsdfg  			TTTT TTTT TTTT", 0), 
				getWikiContent("A", "TTTT new york new york new york new york new york new york", 0), 
				getWikiContent("A", "the white, the blue,....( and the red )", 0), 
				getWikiContent("A", "red blue", 0), 
				getWikiContent("A", "blue red", 0)};
		{
			// Query = ((("new york") and ("TTTT")) or ("0")) and (not ("new"))
			ISearchExpression expr = new AndSearchExpression(new OrSearchExpression(new AndSearchExpression(new TextSearchExpression("new york"), new TextSearchExpression("TTTT")), new TextSearchExpression("0")), new NotSearchExpression(new TextSearchExpression("new")));
			int[] scores = {-1, -2, 0, -1, -6, 0, 0, 0};
			for (int i=0; i < scores.length; ++i )
				assertEquals(scores[i], wr.rank(contents[i], expr));
		}		
	}	
}
